package cn.wolfcode.crm.util;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice //该注解是controller增强切面
public class ExceptionsHandler {

    @org.springframework.web.bind.annotation.ExceptionHandler({UnauthorizedException.class})
    public void exceptionHandler(HandlerMethod method, HttpServletResponse resp) throws IOException {
        ResponseBody body = method.getMethodAnnotation(ResponseBody.class);
        if (body != null) {
            resp.setContentType("text/json;charset=UTF-8");
            resp.getWriter().write("{\"success\":false,\"msg\":\"没有权限执行当前操作\",\"total\":0,\"rows\":[]}");
        }else{
            resp.setContentType("text/html;charset=UTF-8");
            resp.sendRedirect("/nopermission.jsp");
        }
    }
}
